function results = mimo_capacity_scaling(varargin)
% MIMO容量与天线数量缩放分析
% 输入参数:
%   'antenna_configs' - 天线配置数组, 默认: [1, 2, 4, 8, 16]
%   'scaling_snr' - 分析所用的SNR (dB), 默认: 15
%   'num_realizations' - 蒙特卡洛仿真次数, 默认: 500
% 输出:
%   results - 包含容量缩放结果、理论比较等信息的结构体

% 解析输入参数
p = inputParser;
addParameter(p, 'antenna_configs', [1, 2, 4, 8, 16]);
addParameter(p, 'scaling_snr', 15);
addParameter(p, 'num_realizations', 500);
parse(p, varargin{:});

antenna_configs = p.Results.antenna_configs;
scaling_snr = p.Results.scaling_snr;
num_realizations = p.Results.num_realizations;
snr_linear = 10^(scaling_snr/10);

% 添加路径
addpath('../Common');

% 获取颜色定义
colors = color_definitions();

fprintf('=== MIMO容量与天线数量缩放分析 ===\n');

% 容量缩放分析
scaling_capacity = zeros(length(antenna_configs), 2);

for ant_idx = 1:length(antenna_configs)
    Nt = antenna_configs(ant_idx);
    Nr = antenna_configs(ant_idx);
    
    % 蒙特卡洛仿真
    capacities = zeros(num_realizations, 1);
    for real = 1:num_realizations
        H_scaling = sqrt(0.5) * (randn(Nr, Nt) + 1i * randn(Nr, Nt));
        capacities(real) = log2(det(eye(Nr) + (snr_linear/Nt) * H_scaling * H_scaling'));
    end
    
    scaling_capacity(ant_idx, 1) = mean(capacities);
    scaling_capacity(ant_idx, 2) = std(capacities);
end

% 绘制容量缩放
figure('Name', 'MIMO容量缩放', 'Position', [200, 200, 1000, 600]);

subplot(1,2,1);
plot(antenna_configs, scaling_capacity(:, 1), 'o-', 'LineWidth', 2);
hold on;
errorbar(antenna_configs, scaling_capacity(:, 1), scaling_capacity(:, 2), 'LineWidth', 2);
grid on;
xlabel('天线数量');
ylabel('容量 (bps/Hz)');
title(sprintf('MIMO容量 vs 天线数量 (SNR=%d dB)', scaling_snr));

% 理论缩放比较
subplot(1,2,2);
theoretical_scaling = antenna_configs .* log2(1 + snr_linear);
plot(antenna_configs, scaling_capacity(:, 1), 'o-', 'LineWidth', 2);
hold on;
plot(antenna_configs, theoretical_scaling, 'r--', 'LineWidth', 2);
grid on;
xlabel('天线数量');
ylabel('容量 (bps/Hz)');
title('容量缩放: 仿真 vs 理论');
legend('仿真结果', '理论缩放');

% 组织结果
results.antenna_configs = antenna_configs;
results.scaling_capacity = scaling_capacity;
results.theoretical_scaling = theoretical_scaling;
results.scaling_snr = scaling_snr;

end